﻿Ext.data.HasManyAssociation = Ext.extend(Ext.data.Association, {








    constructor: function (config) {
        Ext.data.HasManyAssociation.superclass.constructor.apply(this, arguments);


        var ownerProto = this.ownerModel.prototype, //主model的原型对象
            name = this.name;                       //从model的name属性

        Ext.applyIf(this, {
            storeName: name + "Store",                       //从model的名称（非注册名）
            foreignKey: this.ownerName.toLowerCase() + "_id" //外键名称
        });

        ownerProto[name] = this.createStore(); //可以创建一个Ext.data.Store对象的方法
    },


    createStore: function () {
        var associatedModel = this.associatedModel, //从model的类
            storeName = this.storeName,             //从store的名称（非注册名）
            foreignKey = this.foreignKey,           //外键名
            primaryKey = this.primaryKey,
            filterProperty = this.filterProperty,
            storeConfig = this.storeConfig || {};

        return function () {
            var me = this,
                config, filter,
                modelDefaults = {};

            // 如果还没创建从store
            if (me[storeName] == undefined) {

                // 用于筛选的属性
                if (filterProperty) {
                    filter = {
                        property: filterProperty,
                        value: me.get(filterProperty),
                        exactMatch: true
                    };
                } else {
                    filter = {
                        property: foreignKey,
                        value: me.get(primaryKey),
                        exactMatch: true
                    };
                }

                modelDefaults[foreignKey] = me.get(primaryKey);

                config = Ext.apply({}, storeConfig, {
                    model: associatedModel,
                    filters: [filter],
                    remoteFilter: false,
                    modelDefaults: modelDefaults
                });

                me[storeName] = new Ext.data.Store(config); // 创建存储对象
            }

            return me[storeName];
        };
    }
});